package auth.exception;

import com.wu.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;

@Slf4j
@ControllerAdvice(basePackages = "com.wu.gumomallproduct.product.controller")
public class GulimallExceptionControllerAdvice {

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R HandleVaildException(MethodArgumentNotValidException e){
        log.error("数据校验出现问题{},异常类型:{}",e.getMessage(),e.getClass());
        HashMap<String, String> hashMap = new HashMap<>();
        e.getBindingResult().getFieldErrors().forEach(fieldError -> {
            hashMap.put(fieldError.getField(), fieldError.getDefaultMessage());
        });
        return R.error(400, "数据校验出现问题").put("data", hashMap);
    }

    @ExceptionHandler(value = Throwable.class)
    public R HandleException(Throwable e){
        log.error("数据校验出现问题{},异常类型:{}",e.getMessage(),e.getClass());

        return R.error();
    }


}
